package com.yand.serviceimpl.system;

import com.yand.api.entity.system.TSysMenu;
import com.yand.api.service.system.MenuService;
import com.yand.api.util.TreeUtil;
import com.yand.mapper.system.TSysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MenuServiceImpl implements MenuService {


    @Autowired
    private TSysMenuMapper menuMapper;


    @Override
    public List<TSysMenu> queryMenuList() {
        return menuMapper.queryMenuList();
    }



    @Override
    public List<TSysMenu> convertTreeList(List<TSysMenu> allMenuList) {
        return (List<TSysMenu>)TreeUtil.getRecursionList(allMenuList);
    }

    /**
     * 获取菜单对象
     * @param menuId
     * @return
     */
    @Override
    public TSysMenu getMenu(Integer menuId) {

        if(menuId == 0){
            TSysMenu menu = new TSysMenu();
            menu.setId(0);
            menu.setMenuName("根节点");
            return menu;
        }
        return menuMapper.selectByPrimaryKey(menuId);
    }


    /**
     * 修改菜单
     * @param menu
     */
    @Override
    public void updateMenu(TSysMenu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
    }


    /**
     * 添加菜单
     * @param menu
     */
    @Override
    public void addMenu(TSysMenu menu) {
        menuMapper.insert(menu);
    }

    /**
     * 删除菜单
     * @param menuId
     */
    @Override
    public void delMenu(int menuId) {
        menuMapper.deleteByPrimaryKey(menuId);
    }

    /**
     * 转换成属性结构
     * @param allMenuList
     * @return
     */
    @Override
    public List<TSysMenu> convertTreeJsonList(List<TSysMenu> allMenuList) {

        return  (List<TSysMenu>)TreeUtil.getAllTreeJavaBean(allMenuList);
    }


}
